from driver_school_after.utils import is_blank
from rest_framework.decorators import api_view
from login.models import Coach_info
from django.core.paginator import Paginator
from django.http import JsonResponse


# Create your views here.
@api_view(["get"])
def coach_list(request):
    page = request.GET.get('page')
    count = request.GET.get('count')
    coach_name = request.GET.get('coach_name')
    data = {}

    if not is_blank(coach_name):
        data["coach_name__contains"] = coach_name

    if is_blank(page) or int(page) < 1:
        page = 1
    if is_blank(count):
        count = 10

    coach = Coach_info.objects.filter(**data).values()
    pageobject = Paginator(coach, count)
    pageobject_data = pageobject.page(page)
    info = []

    for item in pageobject_data.object_list:
        info.append(item)

    return JsonResponse({"status": 200, "message": "查询成功", "data": info, "total": coach.count()})
